package cn.geapi.core.filter.aop;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

import cn.geapi.core.datasource.DataSourceHolder;
import cn.geapi.core.datasource.annotation.DataSource;

public class ExchangeDataSourceAOP implements MethodBeforeAdvice {

	public void before(Method method, Object[] objects, Object o)
			throws Throwable {
		DataSource annotation = o
				.getClass()
				.getDeclaredMethod(method.getName(), method.getParameterTypes())
				.getAnnotation(DataSource.class);
		if (null != annotation) {
			DataSourceHolder.setDataSource(annotation.name());
		} else {
			annotation = o.getClass().getAnnotation(DataSource.class);
			if (null != annotation) {
				DataSourceHolder.setDataSource(annotation.name());
			}
		}
	}

}
